package com.example.springloud.controller;

import cn.hutool.core.util.IdUtil;
import com.example.springloud.client.HystrixClient;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chensixiang chensixiang1234@gmail.com
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "globalHandler")
public class OrderController {

    private final HystrixClient hystrixClient;

    @GetMapping("/ok/{id}")
    public String ok(@PathVariable("id") Integer id) {
        return hystrixClient.paymentInfo_OK(id);
    }

    @GetMapping("/timeout/{id}")
    // @HystrixCommand(
    //         fallbackMethod = "paymentTimeoutFallbackMethod",
    //         commandProperties = {
    //                 @HystrixProperty(
    //                         name = "execution.isolation.thread.timeoutInMilliseconds",
    //                         value = "5000"
    //                 )
    //         }
    // )
    // @HystrixCommand
    public String timeout(@PathVariable("id") Integer id) {
        // int i = 10 /0;
        return hystrixClient.paymentInfo_Timeout(id);
    }

    public String paymentTimeoutFallbackMethod(@PathVariable("id") Integer id) {
        return "我是消费者80，对方的支付系统繁忙，请稍后再试！😘";
    }

    public String globalHandler() {
        return "全局异常处理";
    }

}
